<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { MotionComponent } from '@vueuse/motion'

import AntdIcon from '@/components/AntdIcon/index.vue'
import { validPhone } from '@/utils/valid'
import { useCommonStore, useUserStore } from '@/stores'
import { loginApi } from '@/service'
const router = useRouter()
const formRef = ref()
const formState = ref({
  phone_number: '',
  password: '',
})
const rules = {
  phone_number: [{ validator: validPhone, trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}
const login = () => {
  const { setMenuKey, setSlideList } = useCommonStore()
  const { setUserInfo } = useUserStore()
  formRef.value.validate().then(() => {
    const params = {
      ...formState.value,
    }
    loginApi(params).then((res) => {
      setMenuKey(['record'], ['record'])
      setSlideList('预约记录')
      setUserInfo(res)
      router.push({ name: 'record' })
    })
  })
}
</script>

<template>
  <div class="login-page">
    <div class="login-form">
      <MotionComponent preset="slideVisibleLeft">
        <div class="flex justify-center pb-10">
          <img src="@/assets/logo.jpg" alt="" class="h-20" />
        </div>
      </MotionComponent>
      <!-- <MotionComponent preset="slideVisibleLeft">
        <div class="login-title">
          <div class="title">欢迎回来！</div>
          <div class="tips"></div>
        </div>
      </MotionComponent> -->
      <a-form ref="formRef" :model="formState" :rules="rules" autocomplete="off">
        <MotionComponent preset="slideVisibleTop" delay="50">
          <a-form-item name="phone_number">
            <a-input v-model:value="formState.phone_number" placeholder="请输入您的手机号">
              <template #prefix> <AntdIcon icon="PhoneOutlined" /> </template>
            </a-input>
          </a-form-item>
        </MotionComponent>
        <MotionComponent preset="slideVisibleTop" delay="100">
          <a-form-item name="password">
            <a-input-password v-model:value="formState.password" placeholder="请输入您的账户密码">
              <template #prefix> <AntdIcon icon="LockOutlined" /> </template>
            </a-input-password>
          </a-form-item>
        </MotionComponent>
      </a-form>
      <div class="pt-5">
        <a-button class="w-full" type="primary" @click="login"> 登录 </a-button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  width: 100vw;
  height: 100vh;
  position: relative;
  background: url('@/assets/icons/login-bg.webp') no-repeat center center fixed;
  .login-form {
    width: 30%;
    min-width: 300px;
    max-width: 400px;
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 16px;
    border-radius: 10px;
    background-color: #fff;
    border: 1px solid #eee;
    .login-title {
      gap: 16px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .title {
        font-size: 24px;
        font-weight: bold;
        color: var(--color);
      }
    }
    :deep(.ant-input-affix-wrapper),
    :deep(.ant-input-number-wrapper),
    :deep(.ant-btn) {
      height: 44px;
      border-radius: 5px;
    }
    .ant-input-number-group-wrapper {
      width: 100%;
      :deep(.ant-input-number-group-addon) {
        border: none;
        outline: none;
        cursor: pointer;
      }
    }
  }
}
</style>
